package com.huatech.config;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.password.AbstractPasswordEncoder;


/**
 * 
 * @author lihua_java@163.com
 *
 */
public class MD5PasswordEncoder extends AbstractPasswordEncoder {


	@Override
	public boolean matches(CharSequence rawPassword, String encodedPassword) {
		String md5pwd = encode(rawPassword);
		return md5pwd.equalsIgnoreCase(encodedPassword);
	}
	
	@Override
	public String encode(CharSequence rawPassword) {
		try {
			return new String(Hex.encode(MessageDigest.getInstance("MD5").digest(rawPassword.toString().getBytes())));
		} catch (NoSuchAlgorithmException e) {
			return rawPassword.toString();
		}
	}
	
	@Override
	protected byte[] encode(CharSequence rawPassword, byte[] salt) {
		return Hex.decode(encode(rawPassword));
	}

}
